package com.jiandaoshou.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiandaoshou.R;
import com.jiandaoshou.utils.ViewUtils;

/**
 * Created by Administrator on 2016/9/26.
 */

public abstract class BaseViewActivity extends AppCompatActivity {
    private RelativeLayout layout;
    private LinearLayout contentLayout;
    private Toolbar toolbar;
    private ProgressBar progressBar;
    protected TextView toolbarTitleTextView;
    protected Context context;

    @Override
    protected final void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initContentView();
        init();
    }

    /**
     * 主界面组成：
     * 最外层是RelativeLayout，包裹着一个主布局和ProgressBar
     * 主布局是LinearLayout，上下结构，上面是Toolbar，下面是写的布局文件
     */
    private void initContentView() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        contentLayout = new LinearLayout(this);
        contentLayout.setLayoutParams(params);
        contentLayout.setOrientation(LinearLayout.VERTICAL);
        View view = setContentView();
        if (isShowTitle()) {
            initToolbar();
            contentLayout.addView(toolbar);
        }
        contentLayout.addView(view, params);
        layout = ViewUtils.viewAddProgress(this, contentLayout);
        progressBar = (ProgressBar) layout.getTag();
        setContentView(layout);
    }

    public boolean isShowTitle() {
        return true;
    }

    private void initToolbar() {
        toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.toolbar, null);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            //设置成false的话，会隐藏掉返回键
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
        if (showBackLogo()) {
            getToolbar().setNavigationIcon(R.drawable.selector_tab_btn);
        }
        initTitleText();
    }

    private void initTitleText() {
        String title = getTitleText();
        if (TextUtils.isEmpty(title))
            return;
        toolbarTitleTextView = new TextView(this);
        toolbarTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        toolbarTitleTextView.setTextColor(Color.WHITE);
        toolbarTitleTextView.setText(title);
        Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        toolbar.addView(toolbarTitleTextView, params);
    }

    public abstract boolean showBackLogo();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onKeyDown(KeyEvent.KEYCODE_BACK, null);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private View setContentView() {
        if (getContentViewRes() == 0 && getContentView() == null) {
            return null;
        } else if (getContentViewRes() != 0) {
            return LayoutInflater.from(this).inflate(getContentViewRes(), null);
        } else if (getContentView() != null) {
            return getContentView();
        }
        return null;
    }

    /**
     * 获取主布局文件
     *
     * @return
     */
    protected abstract
    @LayoutRes
    int getContentViewRes();

    /**
     * 如果需要设置View类型的布局，需要重写这个方法，并且上个方法返回0
     *
     * @return
     */
    protected View getContentView() {
        return null;
    }

    /**
     * 设置标题栏文字
     *
     * @return
     */
    protected abstract String getTitleText();

    /**
     * 初始化内容
     */
    public abstract void init();

    //////////////////////////////////////////////////下面是操作标题栏公开的方法
    protected final Toolbar getToolbar() {
        return toolbar;
    }

    protected final ProgressBar getProgressBar() {
        return progressBar;
    }
}
